<?php

autoLoader();

$mid 	= ( ( true == isset( $_GET['mid'] ) && true == valStr( $_GET['mid'] ) ) ? $_GET['mid'] : NULL );
$smid 	= ( ( true == isset( $_GET['smid'] ) && true == valStr( $_GET['smid'] ) ) ? $_GET['smid'] : NULL );
$emp_id = ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) && true == valStr( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : NULL;

if( false == valStr( $mid ) ) $mid = ( ( true == isset( $_POST['mid'] ) && true == valStr( $_POST['mid'] ) ) ? $_POST['mid'] : NULL );
if( false == valStr( $smid ) ) $smid = ( ( true == isset( $_POST['smid'] ) && true == valStr( $_POST['smid'] ) ) ? $_POST['smid'] : NULL );

$strAction = ( ( true == isset( $_GET['action'] ) && true == valStr( $_GET['action'] ) ) ? $_GET['action'] : NULL );

	$intPatientId 			= '';
	$intCasePaperId			= '';
	$strName 				= '';
	
	$objPatient 	= new Patients();
	$objCasePaper 	= new CasePapers();
	
	if( true == isset( $_GET['patient_id'] ) && true == valStr( $_GET['patient_id'] ) ) {
		$intPatientId 	= ( int ) $_GET['patient_id'];
		$objPatient = $objPatient->fetchPatientsById( $intPatientId );
	}
	
	if( true == isset( $_GET['case_paper_id'] ) && true == valStr( $_GET['case_paper_id'] ) ) {
		$objCasePaper = $objCasePaper->fetchCasePapersById( ( int ) $_GET['case_paper_id'] );
		if( true == valObj( $objCasePaper, 'BaseCasePapers' ) ) {
			$intCasePaperId = $objCasePaper->getId();
		}
	}
	
	$arrobjPatientNotes = array();
	
	if( true == valObj( $objPatient, 'BasePatients' ) ) {
		$objPatientNote = new PatientNotes();
		
		$strSql = 'SELECT * FROM patient_notes WHERE patient_id = ' . $objPatient->getId() . ';';
		$arrobjPatientNotes = $objPatientNote->fetchSql( $strSql );
	}
?>
<div id="yellow_page_data">
	<div style="margin-top:5px; margin-bottom: 10px;">
		<?php
			if( true == valObj( $objPatient, 'BasePatients' ) ) {
				echo '<input type="button" value="Add New Note" name="add_yellow_pages" onClick="addNewNote();" class="btn btn-primary" />&nbsp;&nbsp;';
				echo '<input type="button" value="View Yellow Pages" name="yellow_pages" onClick="viewAllNotes();" class="btn btn-primary" />&nbsp;&nbsp;';
				echo '<a href="modules.php?file=print_case_sheet&type=print&action=print&patient_id=' . $intPatientId . '&case_paper_id=' . $intCasePaperId . '&mid=' . $mid . '&smid=' . $smid . '"><input type="button" value="Print Case Sheet" name="case_sheet" class="btn btn-primary" /></a>';
			}
		?>
	</div>
	<div style="border-style: solid; display: block;">
		<div style="padding: 2px;display: none;margin:5px 5px 0px 5px;" class="alert alert-success" id="admission_success_display">
			<img alt="admission_insert_success Found with info.." src="images/ok.png" width="20" height="20">
			<label id='admission_insert_success'></label>
		</div>
		
		<div style="padding: 2px;display: none;margin:5px 5px 0px 5px;" class="alert alert-danger" id="admission_failed_display">
			<img alt="Patient Found with info.." src="images/close.png" width="20" height="20">
			<label id='admission_insert_failed'></label>
		</div>
		
		<div style="clear: both;"></div>
			<table width="100%" cellspacing="0" cellpadding="3" style="margin-left: px;" class="table table-striped table-bordered table-hover dataTable no-footer">
				<tr valign="top">
					<td class="mandatory">Id</td>
					<td>Note Type</td>
					<td>Description</td>
					<td>Medical Details</td>
					<td>Doctor By</td>
					<td>Created On</td>
				</tr>
			<?php
				if( true == valArr( $arrobjPatientNotes ) ) {
					foreach( $arrobjPatientNotes as $objPatientNote ) {
						echo '<tr><td>' . $objPatientNote->getId() . '</td>';
						echo '<td>' . $objPatientNote->getNoteTypeId() . '</td>';
						echo '<td><a href="#" onClick="updateYellowPage(' . $objPatientNote->getId() . ');">' . $objPatientNote->getDescription() . '</a></td>';
						echo '<td>' . $objPatientNote->getMedicalDetails() . '</td>';
						echo '<td>' . $objPatientNote->getDoctorId() . '</td>';
						echo '<td>' . date( 'd/m/Y', strtotime( $objPatientNote->getCreatedOn() ) ) . '</td></tr>';
					}
				}
			?>
			</table>
	</div>
</div>

<script>
	function addNewNote() {
		var intPatientId =	$('#patient_details').val();
		
		strUrl = "modules.php?file=fetch_add_yellow_pages&type=fetch&patient_id=" + intPatientId;
		
		$.ajax({
			url: strUrl,
			type: "post",
			data: '',
			success: function( returnData ) {
				strCustomerData = returnData;
				
				$('#yellow_page_data').html( strCustomerData );
			},
			error:function() {
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function viewAllNotes() {
		var intPatientId =	$('#patient_details').val();
		
		strUrl = "modules.php?file=fetch_view_yellow_pages&type=fetch&patient_id=" + intPatientId;
		
		$.ajax({
			url: strUrl,
			type: "post",
			data: '',
			success: function( returnData ) {
				strCustomerData = returnData;
				
				$('#yellow_page_data').html( strCustomerData );
			},
			error:function() {
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function updateYellowPage( intPatientNoteId ) {

		var intPatientId =	$('#patient_details').val();
		
		strUrl = "modules.php?file=fetch_add_yellow_pages&type=fetch&patient_id=" + intPatientId + "&patient_note_id=" + intPatientNoteId;
		
		$.ajax({
			url: strUrl,
			type: "post",
			data: '',
			success: function( returnData ) {
				strCustomerData = returnData;
				
				$('#yellow_page_data').html( strCustomerData );
			},
			error:function() {
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}
</script>

<?php
	if( true == valStr( $strAction ) ) {
		echo '<script>addNewNote();</script>';
	}
?>